package com.atguigu.gulimall.search.controller;

import com.atguigu.common.exception.BizCodeEnume;
import com.atguigu.common.utils.R;
import com.atguigu.entity.to.es.SkuESModel;
import com.atguigu.gulimall.search.service.ProductSearchSaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("search/save")
public class ProductSearchSaveController {

    @Autowired
    ProductSearchSaveService productSearchSaveService;

    @RequestMapping("/product")
    public R saveProduct(@RequestBody List<SkuESModel> eSModels){

        try {
            boolean b = productSearchSaveService.saveProduct(eSModels);
            if (b){
                return R.ok();
            }else {
                return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnume.PRODUCT_UP_EXCEPTION.getMsg());
            }
        } catch (IOException e) {
            return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnume.PRODUCT_UP_EXCEPTION.getMsg());
        }
    }
}
